import xml.etree.ElementTree as ET
import re
import csv
import os
from fields_define import fields

def getItem(root, filename):
    item = {}
    for field in fields:
        #field即fields里的key值

        typee = fields[field]['type']
        xpath = fields[field]['xpath']

        if typee == 'common':   
            t = root.findall('.//'+ xpath)
            m = len(t)
            if len(t) == 0:
                continue
            string = ''
            for i in range(0,m):
                string  = string + t[i].text
                if i != m-1:
                    string = string +'|'
            item[field] = string
                

        if typee == 'textblock':
            t = root.findall('.//'+ xpath)
            if len(t) != 0:
                item[field] = re.sub('\\n(\\s)+', ' ', t[0].text)
            if len(t) > 1:
                print(filename + '警告：元素'+field+'出现多于一次')

        if typee == 'attrib':
            t = root.findall('.//'+ xpath)
            if len(t) != 0:
                if 'type' in t[0].attrib:
                    item[field] = t[0].attrib['type']
            if len(t) > 1:
                print(filename + '警告：元素'+field+'出现多于一次')

        if typee == 'intervention':
            intervention_type = root.findall('.//intervention_type')
            intervention_name = root.findall('.//intervention_name')
            m = len(intervention_type)
            if m == 0:
                continue
            if m >= 1:
                string = ''
                for i in range(0,m):
                    string  = string + intervention_type[i].text + ':' + intervention_name[i].text
                    if i != m-1:
                        string = string +'|'
            item[field] = string

        if typee == 'location_facility':
            locations = root.findall('.//location')
            i = 0
            string = ''
            m = len(locations)
            for location in locations:  
                facility_name = ''
                city = ''
                state = ''
                zipp = ''
                country = ''
                try:
                    facility_name = location.findall('.//facility/name')[0].text
                except Exception as e:
                    pass
                try:
                    city = location.findall('.//address/city')[0].text
                except Exception as e:
                    pass
                try:
                    state = location.findall('.//address/state')[0].text
                except Exception as e:
                    pass
                try:
                    zipp = location.findall('.//address/zip')[0].text
                except Exception as e:
                    pass
                try:
                    country = location.findall('.//address/country')[0].text
                except Exception as e:
                    pass

                string  = string + facility_name +','+ city +','+ state +','+ zipp +','+ country
                if i != m-1:
                        string = string +'|'
                i = i + 1

            item[field] = string

        if typee == 'keyword':
            keywords = root.findall('.//keyword')
            m = len(keywords)
            if m == 0:
                continue
            if m >= 1:
                string = ''
                for i in range(0,m):
                    string  = string + keywords[i].text
                    if i != m-1:
                        string = string +'|'
            item[field] = string

        if typee == 'mesh':
            mesh_term = root.findall('.//'+ xpath)
            m = len(mesh_term)
            if m == 0:
                continue
            if m >= 1:
                string = ''
                for i in range(0,m):
                    string  = string + mesh_term[i].text
                    if i != m-1:
                        string = string +'|'
            item[field] = string
    return item


foldernames = os.listdir()
csvfile_name = 'NCT.csv'
csvfile =  open(csvfile_name, 'w', newline='', encoding='utf8')
writer = csv.DictWriter(csvfile, fieldnames=fields)
writer.writeheader()
for foldername in foldernames:
    if not foldername.endswith('xxxx'):
        continue
    print(foldername)
    filenames = os.listdir(foldername)
    for filename in filenames:
        if not filename.endswith('.xml'):
            continue
        try:
            tree = ET.parse(foldername + '/' + filename)
            root = tree.getroot()
            item = getItem(root, foldername + '/' + filename)
            writer.writerow(item)
        except Exception as e:
            print(foldername + '/' + filename + ': ' + str(e))
            continue
csvfile.close()








